﻿Public Class frmBackup

    
    Private Sub cmdChonDuongDan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChonDuongDan.Click
        Try
            FBD.ShowDialog()
            txtPath.Text = FBD.SelectedPath()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtPath_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPath.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackup.Click
        Try
            Dim query_backup As String = ""
            Dim randomcls As New Random
            'check path folder is exits
            If System.IO.Directory.Exists(txtPath.Text) = False Then
                MessageBox.Show("Đường dẫn thư mục không tồn tại.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Question)
                Exit Sub
            End If
            Dim filename As String = txtPath.Text & "SonDongNai_" & Now.Year.ToString & Now.Month.ToString() & Now.Day.ToString() & "_" & randomcls.Next(0, 9999) & ".bak"
            query_backup = "BACKUP DATABASE [SonDongNai] TO " & _
                            " DISK = N'" & filename & "'" & _
                            " WITH NOFORMAT, NOINIT,  NAME = N'SonDongNai-Full Database Backup'," & _
                            " SKIP, NOREWIND, NOUNLOAD, STATS = 10"
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDb.OleDbCommand
            Panel_watting.Visible = True
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDb.OleDbCommand(query_backup, dbConn)
            dbCommand.ExecuteNonQuery()
            Panel_watting.Visible = False
            MessageBox.Show("Đã thực hiện sao lưu hệ thống thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class